package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.CategoryNotFoundException;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;


    /**
     * 新增分类
     *
     * @param categoryDTO
     */
    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        //对象拷贝属性
        BeanUtils.copyProperties(categoryDTO, category);
        category.setStatus(StatusConstant.DISABLE);
/*        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());*/
        categoryMapper.insert(category);
    }

    /**
     * 分类分页查询
     *
     * @param categoryPageQueryDTO 分类分页DTO
     * @return 分类分页结果
     */
    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO){
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page<Category> page= (Page<Category>)categoryMapper.pageQuery(categoryPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根绝类型查询分类
     * @param type 分类类型
     * @return 分类列表
     */
    @Override
    public List<Category> getByType(Integer type){
        return categoryMapper.getByType(type);

    }

    /**
     * 删除分类
     * @param id 分类id
     */
    @Override
    public void deleteById(Long id){
        //此处可以添加防止误删保护（防止分类还在处于启用状态直接删除）
        int affectRows = categoryMapper.deleteById(id);
        if(affectRows == 0){
            log.warn("删除的分类不存在，分类id：{}", id);
            throw new CategoryNotFoundException(MessageConstant.CATEGORY_NOT_FOUND);
        }
    }

    /**
     * 修改分类
     * @oaram categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO){
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
/*        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());*/
        categoryMapper.update(category);
    }

    /**
     * 启用禁用分类
     * @param status 状态
     * @param id 分类id
     */
    @Override
    public void startOrStop(Integer status, Long id){
        Category category = Category.builder()
                .status(status)
                .id(id)
/*                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())*/
                .build();
        categoryMapper.update(category);
    }
}
